package com.FH.permission.role_privilege;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/8/4.
 *
 * 角色权限实现类
 */
@Stateless
public class RolePrivilegeDAOImpl extends AccessDB implements RolePrivilegeDAO {

    /**
     * GET查询方法
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getRolePrivilegeInfo(String search, String page, String size) {


        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(2) from role_privilege where 1=1 ";

            String sql_w ="";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(page);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok + 1) + "," + size + "";
            }

            if(json.has("contract_id")){
                sql_w += " AND contract_id = '"+json.getString("contract_id")+"'";
            }
            if(json.has("member_id")){
                sql_w += " AND member_id = '"+json.getString("member_id")+"'";
            }
            if(json.has("role_privilege_id")){
                sql_w += " AND role_privilege_id = '"+json.getString("role_privilege_id")+"'";

            }
            String sql_ok = sqlexit+sql_w ;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String []> seresult = new ArrayList<String []>();

            List<RolePrivilegeShowDTO> result = new ArrayList<RolePrivilegeShowDTO>();

            if(!existName.equals("0")){

                String sql ="select c.role_privilege_id,c.role_id,c.privilege_id,c.role_privilege_create," +
                        "c.role_privilege_create_time,c.role_privilege_modify,c.role_privilege_modify_time,b.privilege_type,a.sys_role_name from" +
                        " privilege b,sys_role a,role_privilege c where c.role_id=a.sys_role_id AND b.privilege_id=c.privilege_id " +sql_w + sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++){

                    RolePrivilegeShowDTO sysOperatorShowDTO = new RolePrivilegeShowDTO();

                    sysOperatorShowDTO.setRole_privilege_id(seresult.get(i)[1]);
                    sysOperatorShowDTO.setRole_id(seresult.get(i)[2]);
                    sysOperatorShowDTO.setPrivilege_id(seresult.get(i)[3]);
                    sysOperatorShowDTO.setRole_privilege_create(seresult.get(i)[4]);
                    sysOperatorShowDTO.setRole_privilege_create_time(seresult.get(i)[5]);
                    sysOperatorShowDTO.setRole_privilege_modify(seresult.get(i)[6]);
                    sysOperatorShowDTO.setRole_privilege_modify_time(seresult.get(i)[7]);
                    sysOperatorShowDTO.setPrivilege_type(seresult.get(i) [8]);
                    sysOperatorShowDTO.setSys_role_name(seresult.get(i) [9]);
                    result.add(sysOperatorShowDTO);

                }
            }
            if(seresult.size()!=0) {
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            }else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return resultDTO;
    }

    /**
     * 添加角色权限方法
     * @param dataDTO
     * @return
     */
    @Override
    public ResultDTO addRolePrivilegeInfo(RolePrivilegePullDTO dataDTO) {
        ResultDTO resultDTO = new ResultDTO();//构建返回对象

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String time=df.format(new Date());


        if(dataDTO.getPrivilege_id()!=null && dataDTO.getRole_id()!=null){
            String privilege_id=dataDTO.getPrivilege_id();
            String role_id=dataDTO.getRole_id();
            String sql="select * from role_privilege where privilege_id='"+privilege_id+"' and role_id='"+role_id+"'";
            String sum= getReportingToOne(sql);
            if(!sum.equals("")){
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion("角色已存在请重新分配角色");
                return resultDTO;
            }
        }

        String sql = "";

        sql += "insert into role_privilege(role_privilege_id,role_id,privilege_id," +
                "role_privilege_create,role_privilege_create_time,role_privilege_modify,role_privilege_modify_time) values" +
                " ('" + uuid + "','" + dataDTO.getRole_id() + "','" + dataDTO.getPrivilege_id() + "','" + dataDTO.getRole_privilege_create() + "','" + time + "','" + dataDTO.getRole_privilege_modify() + "','" + time + "')";

        int result = this.addReportingTo(sql);

        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }
}
